
/**
 * 画线操作
 * @param {} pen
 * color 颜色
 * weight 线宽
 * arrows 是否带箭头
 */
ECOLine = function(pen) {
	pen = pen || {};
	pen.color = pen.color || '#11A709';
	pen.weight = pen.weight || 8;//线的宽度
	pen.arrowsDefaultW = 14;//箭头默认宽度
	pen.arrowsDefaultH = 13;//箭头默认高度
	pen.defaultColor = '#11A709';
	pen.arrowsW = pen.arrowsW || pen.arrowsDefaultW;//箭头宽度
	pen.arrowsH = pen.arrowsH || pen.arrowsDefaultH;//箭头宽度
	
	this.pen = pen;
};

ECOLine.prototype = {
	/**
	 * 重新设置画笔
	 * @param {} pen
	 */
	setPen : function(pen){
		if(!pen){
			this.pen = pen;
		}
	},
	
	/**
	 * 设置画笔颜色
	 * @param {} color
	 */
	setColor : function(color){
		if(color){
			this.pen.color = color;
		}
	},
	
	/**
	 * 设置线的宽度
	 * @param {} weight
	 */
	setLineWeight : function(weight){
		if(weight){
			this.pen.weight = weight;
		}
	},
	
	/**
	 * 设置箭头大小
	 * @param {} zoom 比例因子
	 */
	setArrowsSize : function(zoom){
		if(zoom){
			this.pen.arrowsW = this.pen.arrowsDefaultW * zoom;
			this.pen.arrowsH = this.pen.arrowsDefaultH * zoom;
		}
	},
	
	/**
	 * 设置线箭头图标
	 * @param {} arrowsIcon
	 */
	setArrowsIcon : function(arrowsIcon){
		if(arrowsIcon){
			this.pen.arrowsIcon = arrowsIcon;
		}
	},
	
	/**
     * 画线，目前只支持水平和垂直绘制
     * @param {} config
     * config 中有以下属性
     * @param {} id 节点id
     * @param {} x0 开始x坐标
     * @param {} y0 开始y坐标
     * @param {} x1 结束x坐标
     * @param {} y1 结束y坐标
     * @param {} arrows 是否显示箭头
     * @param {} adjustA 是否调整箭头位置
     */
    drawLine : function(config) {
		if(!config || !config.x0 || !config.y0 || !config.x1 || !config.y1){
			return '';
		}
		var id = config.id, x0 = config.x0, y0 = config.y0, x1 = config.x1, y1 = config.y1,
		    arrows = config.arrows, adjustA = config.adjustA,
		    s = [],
            lineColor = this.pen.color,
            lineWeight = this.pen.weight,
            arrowsW = this.pen.arrowsW,
            arrowsH = this.pen.arrowsH,
            arrowsIcon = this.pen.arrowsIcon;

        // For Horizontal line
        if (y0 == y1) {
            if (arrows === true) {// 箭头
                s.push('<div id="arrows_' + id + '" style="position:absolute;overflow:hidden;left:' + (x0 < x1 ? x0 : x1)
                        + 'px;top:' + (y0 - lineWeight / 2) + 'px;width:'
                        + (Math.abs(x1 - x0) + 1 - arrowsW / 2 - 3) + 'px;height:' + lineWeight
                        + 'px;background-color:' + lineColor + '"></div>');
                
                var adjustX, adjustY;
                adjustX = x0 > x1 ? x0 : x1;
                adjustX -= arrowsW;
                adjustY = y0 - arrowsW / 2 + 1;
                  s.push('<div id="' + id + '" style="position:absolute;overflow:hidden;left:' + adjustX + 'px;top:'
                        + (adjustY + (adjustA ? -1 : 0) )+ 'px;"><img width="' + arrowsW + 'px" height="' + arrowsH + 'px" src="' + ECOTree.IMAGE_PATH
                        + 'images/' + arrowsIcon + '"></div>');
                
                
            } else {
                s.push('<div id="' + id + '" style="position:absolute;overflow:hidden;left:' + (x0 < x1 ? x0 : x1)
                        + 'px;top:' + (y0 - lineWeight / 2) + 'px;width:' + (Math.abs(x1 - x0) + 1)
                        + 'px;height:' + lineWeight + 'px;background-color:' + lineColor
                        + '"></div>');
            }
        }

        // For Vertical line
        if (x0 == x1) {
        	s.push('<div id="' + id + '" style="position:absolute;overflow:hidden;left:' + x0 + 'px;top:' + (y0 < y1 ? y0 : y1)
                    + 'px;width:' + lineWeight + 'px;height:' + (Math.abs(y1 - y0) + 1)
                    + 'px;background-color:' + lineColor + '"></div>');
        }
        return s.join('');
    }
};
